<?php

namespace app\common\model;

use think\Db;

/**
 * Class Auth
 * @package app\common\model
 * 巴士路线的线路班次缓存表
 */
class trClassesCache
{
    static $name = "classes_cache"; //表名

    //根据条件获取数量
    public static function getCount($filter){
        if(!empty($filter)){
            return Db::name(self::$name)->where($filter)->count();
        }else{
            return Db::name(self::$name)->count();
        }
    }

    //保存
    public static function toSave($value = "", $key = "", $extra = []){
        if(empty($value) || !$key)
            return false;
        $mall_id = request()->mallData['id'];
        if((C_get("{$mall_id}_isExist_{$key}")) !== false || self::getCount(["key" => $key, "mall_id" => $mall_id])) { //更新
            $result = Db::name(self::$name)->where("key", $key)->where("mall_id", $mall_id)->update(array_merge($extra, ["value" => serialize($value)]));
            if ($result) {
                C_set("{$mall_id}_".$key, $value);
                C_set("{$mall_id}_isExist_{$key}", true);
                return true;
            }
            if ($result === 0) {
                return true;
            }
        }else{
            if(Db::name(self::$name)->insert(array_merge($extra, ["value" => serialize($value), "key" => $key, "mall_id" => $mall_id]))){
                C_set("{$mall_id}_".$key, $value);
                C_set("{$mall_id}_isExist_{$key}", true);
                return true;
            }
        }
        return false;
    }

    //获取某字段的值
    public static function getValue($key){
        if (empty($key)) {
            return false;
        }
        $mall_id = request()->mallData['id'];
        if(($data = C_get("{$mall_id}_".$key)) === false){
            $item = Db::name(self::$name)->where("key", $key)->where("mall_id", $mall_id)->field("value")->find();
            if(!empty($item["value"])) {
                $data = unserialize($item["value"]);
                C_set("{$mall_id}_".$key, $data);
                C_set("{$mall_id}_"."isExist_{$key}", true);
            }
        }
        return $data;
    }

    //根据删除数据
    public static function del($key){
        $mall_id = request()->mallData['id'];
        if (!is_array($key)) {
            C_set("{$mall_id}_".$key, null);
            $filter = ["key" => $key, "mall_id" => $mall_id];
            C_set("{$mall_id}_"."isExist_{$key}", null);
        }else{
            foreach ($key as $id){
                C_set("{$mall_id}_".$id, null);
                C_set("{$mall_id}_"."isExist_{$id}", null);
            }
            $filter = [["key", "in", $key], ["mall_id", "=", $mall_id]];
        }
        $result = Db::name(self::$name)->where($filter)->delete();
        return $result;
    }

    //根据条件删除数据
    public static function delByFilter($where){
        if(isset($where[0])){
            $where[] = ["mall_id", "=", request()->mallData['id']];
        }else{
            $where["mall_id"] = request()->mallData['id'];
        }
        $key = Db::name(self::$name)->where($where)->column("key");
        if (!is_array($key)) {
            C_set(request()->mallData['id']."_".$key, null);
            C_set(request()->mallData['id']."_"."isExist_{$key}", null);
        }else{
            foreach ($key as $id){
                C_set(request()->mallData['id']."_".$id, null);
                C_set(request()->mallData['id']."_"."isExist_{$id}", null);
            }
        }
        $result = Db::name(self::$name)->where($where)->delete();
        return $result;
    }
}